package com.huluobo.a2201mvvm_imdemo.chat;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2201mvvm_imdemo.mvvm.BaseRepo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;

/**
 * Created by LC on 2024/4/9.
 */
public class ChatRepo extends BaseRepo {
    public void sendMessage(String content, String username, int chatType, MutableLiveData<EMMessage> success,
                            MutableLiveData<String> failed) {
        //构建出一条消息
        EMMessage emMessage = EMMessage.createTextSendMessage(content, username);
        //设置消息类型
        switch (chatType) {
            case 1:
                emMessage.setChatType(EMMessage.ChatType.Chat);
                break;
            case 2:
                emMessage.setChatType(EMMessage.ChatType.GroupChat);
                break;
            case 3:
                emMessage.setChatType(EMMessage.ChatType.ChatRoom);
                break;
        }

        //设置消息回调
        emMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue(emMessage);
            }

            @Override
            public void onError(int code, String error) {
                failed.postValue("发送消息失败:" + error + ",code" + code);
            }
        });

        //通过环信把消息发送出去
        EMClient.getInstance().chatManager().sendMessage(emMessage);
    }
}
